﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Data.SqlClient;
using System.Configuration;

namespace AE_TimeSheet_EmailNotification
{
    public class EmailNotification
    {
        static void Main(string[] args)
        {
            string sFuncName = string.Empty;
            string sProcName = string.Empty;
            string sErrorDesc = string.Empty;
            string sReturnResult = string.Empty;
            Int16 DEBUG_ON = 1;
            Int16 DEBUG_OFF = 0;
            Int16 p_iDebugMode = DEBUG_ON;
            clsLog oLog = new clsLog();
            try
            {
                sFuncName = "Main Function()";

                if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("Starting Function ", sFuncName);
                Console.WriteLine("Starting Function " + sFuncName);
                string sConnectionString = string.Empty;
                if (string.IsNullOrEmpty(sConnectionString))
                {
                    if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("Before Initializing Connection string ", sFuncName);
                    Console.WriteLine("Before Initializing Connection string ");
                    string connectionName = ConfigurationManager.AppSettings["ConnectionName"];

                    sConnectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
                    if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("After Initializing Connection string ", sFuncName);
                    Console.WriteLine("After Initializing Connection string ");
                }

                SqlConnection conn = new SqlConnection(sConnectionString);
                Console.WriteLine("Before Open the Connection ");
                if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("Before Open the Connection", sFuncName);

                conn.Open();
                Console.WriteLine("After Open the Connection ");
                if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("After Open the Connection", sFuncName);

                sProcName = "AE_SP001_TimeSheet_EmailNotification";
                SqlDataAdapter da = new SqlDataAdapter();
                Console.WriteLine("Before Executing the Procedure '" + sProcName + "'");
                if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("Before Executing the Procedure '" + sProcName + "'", sFuncName);

                da.SelectCommand = new SqlCommand("AE_SP001_TimeSheet_EmailNotification", conn);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;

                if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("After Executing the Procedure '" + sProcName + "'", sFuncName);
                Console.WriteLine("After Executing the Procedure '" + sProcName + "'");
                DataSet ds = new DataSet();
                Console.WriteLine("Before Filling the Dataset ");
                if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("Before Filling the Dataset", sFuncName);

                da.Fill(ds, "GetUserDetails");
                Console.WriteLine("After Filling the Dataset ");
                if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("After Filling the Dataset", sFuncName);

                SendEmail oSendEmail = new SendEmail();
                sFuncName = "SendAutomatedEmail";
                Console.WriteLine("Before Calling the Method " + sFuncName);
                if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("Before Calling the Method " + sFuncName, sFuncName);
                if (ds != null && ds.Tables.Count > 0)
                {
                   sReturnResult = oSendEmail.SendAutomatedEmail(ds);
                }
                else
                {
                    if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("There is no data to send Email on the Last week", sFuncName);
                    Console.WriteLine("There is no data to send Email");
                }
                if (sReturnResult == string.Empty)
                {
                    if (p_iDebugMode == DEBUG_ON) oLog.WriteToDebugLogFile("Emails Sent Successfully", sFuncName);
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                sErrorDesc = ex.Message.ToString();
                Console.WriteLine(ex.Message.ToString());
                if (p_iDebugMode == DEBUG_ON) oLog.WriteToErrorLogFile(sErrorDesc, sFuncName);
                if (p_iDebugMode == DEBUG_ON) oLog.WriteToErrorLogFile("Completed with error ", sFuncName);
                throw ex;
            }
        }
    }
}
